package kr.co.pension.admin.bd.mg.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.pension.admin.bd.mg.service.BDMGService;
import kr.co.pension.admin.bd.mg.vo.BDMGVo;
import kr.co.pension.admin.co.ma.service.COMAService;
import kr.co.pension.admin.pe.ti.service.PETIService;
import kr.co.pension.admin.pe.ti.vo.PETIVo;
import kr.co.pension.utils.BaseController;
import kr.co.pension.utils.ConstValue;
import kr.co.pension.utils.PaginationInfo;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/pmgr/bd/mg/*")
public class BDMGController extends BaseController{
	@Autowired
	private BDMGService service;
	
	@Autowired
	private PETIService petiService;
	
	@Autowired
	private COMAService comaService;

	Logger log = Logger.getLogger(getClass());
	
	//파일 업로드 폴더 설정
	private final String FILE_PATH= ConstValue.TOP_UPLOAD+ "/peti";
	
	/**
	 * 펜션별 게시판 관리
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView bdmg0101Q(BDMGVo vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		isAdminMember(req,res);
		ModelAndView mav = new ModelAndView();

//		// 페이징 시작
		PaginationInfo paginationInfo = new PaginationInfo();
		int currnetPageNo = vo.getCurrentPageNo() <= 1 ? 1 : (Integer) vo
				.getCurrentPageNo();
		vo.setCurrentPageNo(currnetPageNo);
		vo.setParamMap(requestPram2Map(req));


		int iTotalCnt = service.bdmg0101_cntQ(vo);
		vo.setTotalRecordCount(iTotalCnt);
		
		if(iTotalCnt>0){
			
			// 상위 코드목록
			mav.addObject("list", service.bdmg0101Q(vo));
			mav.addObject("pageHtml", vo.getToHtml("./bdmg0101Q.do"));
			mav.addObject("paginationInfo", vo);
		}
		return mav;
	}
	
	/**
	 * 펜션별 게시판 애용 보기
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView bdmg0201Q(BDMGVo vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		isAdminMember(req,res);
		ModelAndView mav = new ModelAndView();

//		// 페이징 시작
		PaginationInfo paginationInfo = new PaginationInfo();
		int currnetPageNo = vo.getCurrentPageNo() <= 1 ? 1 : (Integer) vo
				.getCurrentPageNo();
		vo.setCurrentPageNo(currnetPageNo);
		vo.setParamMap(requestPram2Map(req));


		PETIVo p_idInfo = new PETIVo();
		p_idInfo.setP_id(vo.getP_id());
		mav.addObject("pension", petiService.peti301Q(p_idInfo));
		
		int iTotalCnt = service.bdmg0201_cntQ(vo);
		vo.setTotalRecordCount(iTotalCnt);
		
		if(iTotalCnt>0){
			
			// 상위 코드목록
			mav.addObject("list", service.bdmg0201Q(vo));
			mav.addObject("pageHtml", vo.getToHtml("./peti100Q.do"));
			mav.addObject("paginationInfo", vo);
		}
		return mav;
	}
	
	/**
	 * 신규게시판 등록
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView bdmg0202T(BDMGVo vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		isAdminMember(req,res);
		service.bdmg0202T(vo);
		
		ModelAndView mav = new ModelAndView("/common/close");
		mav.addObject("redirect","./bdmg0201Q.do?p_id="+vo.getP_id());
		mav.addObject("msg","추가되었습니다.");

		
		return mav;
	}
	
	/**
	 * 게시판 수정
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView bdmg0203T(BDMGVo vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		isAdminMember(req,res);

		service.bdmg0203T(vo);
		ModelAndView mav = new ModelAndView("/common/close");
		mav.addObject("redirect","./bdmg0201Q.do?p_id="+vo.getP_id());
		mav.addObject("msg","수정되었습니다.");

		return mav;
	}
	
	/**
	 * 게시판 삭제
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView bdmg0204T(BDMGVo vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		isAdminMember(req,res);

		service.bdmg0204T(vo);
		ModelAndView mav = new ModelAndView("/common/close");
		mav.addObject("redirect","./bdmg0201Q.do?p_id="+vo.getP_id());
		mav.addObject("msg","삭제되었습니다.");

		return mav;
	}
}
